var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		isload:false,
		score: 0,
		content_pic:[]
	},
	_onLoad: function () {
		var that = this;
		var ogid = that.options.ogid;
		$.get('collage/comment', { ogid: ogid }, function (res) {
			res.isload = true
			if(res.comment){
				res.score = res.comment.score
				var content_pic = res.comment.content_pic
				res.content_pic = content_pic.split(',')
			}
			that.setData(res)
		});
	},
	formSubmit: function (e) {
		var that = this;
		var ogid = that.options.ogid
		var score = e.detail.value.score
		var content = e.detail.value.content
		var content_pic = that.data.content_pic
		if (score == 0) {
			$.error('请打分'); return;
		}
		if (content == '') {
			$.error('请填写评价内容'); return;
		}
		$.post('collage/comment', { ogid: ogid, content: content, content_pic: content_pic.join(','), score: score }, function (data) {
			$.success(data.msg);
			setTimeout(function () {
				$.goback(true);
			}, 2000);
		},'提交中');
	},
	handleClick: function(e) {
		if(this.data.comment && this.data.comment.id) return;
		var index = e.currentTarget.dataset.index;
		this.setData({ score: index + 1 })	
	},
	handleTouchMove: function(e) {
		if(this.data.comment && this.data.comment.id) return;
		var clientWidth = wx.getSystemInfoSync().windowWidth;
		if (!e.changedTouches[0]) return;
		var movePageX = e.changedTouches[0].pageX;
		var space = movePageX - 150 / 750 * clientWidth;
		if (space <= 0) return;
		var starwidth = 60 / 750 * clientWidth;
		var setIndex = Math.ceil(space / starwidth);
		setIndex = setIndex > 5 ? 5 : setIndex;
		this.setData({ score: setIndex })
	},
	chooseimage: function () {
		var that = this;
		$.chooseImage(function (data) {
			that.setData({
				tempFilePaths: data
			});
		}, 3)
	}
}))
